package cn.itcast.crm.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	
	@Autowired
	CustomerRepository customerRepository;

	@Override
	public List<Customer> findNoAssociationCustomers() {
		return customerRepository.findByFixedAreaIdIsNull();
		
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
		return customerRepository.findByFixedAreaId(fixedAreaId);
		
	}

	//关联客户与定区
	
	@Override
	public void associationCustomersToFixedArea(String customerIdStr, String fixedAreaId) {
		String[] customerIds = customerIdStr.split(",");
		for (String customerId : customerIds) {
			customerRepository.associationCustomersToFixedArea(Integer.parseInt(customerId),fixedAreaId);
		}
		
		
	}

	//更新外键置null
	@Override
	public void clearFixedAreaId(String fixedAreaId) {
		customerRepository.clearFixedAreaId(fixedAreaId);
		
	}

	

}
